﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.APP_UI.Common;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_ExpenseSource : System.Web.UI.Page
    {
        IExpenseBLL expCtrl = new ExpenseBLL();
        Expense_BLL exp = new Expense_BLL();
        IExpenseGroupBLL expGrpCtrl = new ExpenseGroupBLL();
        ComboBLL comboBLL = new ComboBLL();
        SsMng ssmng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDDLBgmSourceSeq();
                LoadTreeSelectExpense();
            }
        }

        private void LoadTreeSelectExpense()
        {
            treeSelectExpense.DataSource = exp.TreeView();
            treeSelectExpense.DataBind();

            DataTable dtb = exp.GetLeafExpenseSourceData(ddlBgmSourceSeq.SelectedValue);

            foreach (DataRow dr in dtb.Rows)
            {
                foreach (RadTreeNode node in treeSelectExpense.Nodes)
                {
                    CheckRecursive(node, dr["bgm_exp_seq"].ToString());
                }
            }
        }

        private void CheckRecursive(RadTreeNode _node, string strValue)
        {
            if (_node.Value.Split('-')[1] == strValue)
            {
                _node.Checked = true;
            }
            else
            {
                foreach (RadTreeNode node in _node.Nodes)
                {
                    CheckRecursive(node, strValue);
                }
            }
        }

        private void LoadDDLBgmSourceSeq()
        {
            ddlBgmSourceSeq.DataSource = comboBLL.ListSourceCombo();
            ddlBgmSourceSeq.DataBind();

            if (ddlBgmSourceSeq.Items.Count > 0)
            {
                ddlBgmSourceSeq.SelectedIndex = 0;
            }
        }

        protected void ddlBgmSourceSeq_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadTreeSelectExpense();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        protected void cmdSave_Click(object sender, EventArgs e)
        {
            foreach (RadTreeNode node in treeSelectExpense.Nodes)
            {
                SaveRecursive(node, false);
            }
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="_node"></param>
        /// <param name="bSave"></param>
        private void SaveRecursive(RadTreeNode _node, bool bSave)
        {
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strBgmExpSeq = _node.Value;

            if (bSave)
            {
                SaveNode(_node, strBgmExpSeq, strBgmSourceSeq);
            }

            foreach (RadTreeNode node in _node.Nodes)
            {
                //string strBgmExpSeq = node.Value;
                //SaveNode(node, strBgmExpSeq, strBgmSourceSeq);
                SaveRecursive(node, true);
            }
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="node"></param>
        /// <param name="strBgmExpSeq"></param>
        /// <param name="strBgmSourceSeq"></param>
        private void SaveNode(RadTreeNode node, string strBgmExpSeq, string strBgmSourceSeq)
        {
            string strBgmExpSourceSeq = "";
            string strEmpSeq = ssmng.EmpSeq;
            strBgmExpSeq = strBgmExpSeq.Split('-')[1];
            
            DataTable dtb = exp.GetExpenseSourceData(strBgmExpSeq, strBgmSourceSeq);

            if (dtb.Rows.Count > 0)
            {
                strBgmExpSourceSeq = dtb.Rows[0]["bgm_exp_source_seq"].ToString();
            }

            if (node.Checked)
            {
                if (strBgmExpSourceSeq.Length == 0)
                {
                    exp.InsertExpSource(strBgmExpSeq, strBgmSourceSeq, strEmpSeq);
                }
                else
                {
                    exp.ReturnExpSource(strBgmExpSourceSeq, strEmpSeq);
                }
            }
            else
            {
                if (strBgmExpSourceSeq.Length == 0)
                {
                }
                else
                {
                    exp.DeleteExpSource(strBgmExpSourceSeq, strEmpSeq);
                }
            }
        }
    }
}
